From e58bbae1da76d4b4083092a97414e57a87acfc45 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 2 Oct 2005 19:22:25 +0000 Subject: [PATCH] add edit page for hosts and ethers SVN-Revision: 2026 --- openwrt/package/webif/Config.in | 3 +- .../webif/files/usr/lib/webif/apply.sh | 21 ++- .../webif/files/usr/lib/webif/form.awk | 1 + .../webif/files/usr/lib/webif/webif.sh | 24 ++- .../webif/files/www/cgi-bin/webif/config.sh | 70 +++++---- .../webif/files/www/cgi-bin/webif/hosts.sh | 148 ++++++++++++++++++ .../webif/files/www/cgi-bin/webif/upgrade.sh | 1 - openwrt/package/webif/files/www/webif.css | 6 + 8 files changed, 232 insertions(+), 42 deletions(-) create mode 100755 openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh diff --git a/openwrt/package/webif/Config.in b/openwrt/package/webif/Config.in index 079b4d8617..fa96980585 100644 --- a/openwrt/package/webif/Config.in +++ b/openwrt/package/webif/Config.in @@ -1,6 +1,7 @@ config BR2_PACKAGE_WEBIF + select BR2_PACKAGE_HASERL tristate "webif - OpenWrt Administrative Console" - default n + default m if CONFIG_DEVEL help A web interface for configuring OpenWrt diff --git a/openwrt/package/webif/files/usr/lib/webif/apply.sh b/openwrt/package/webif/files/usr/lib/webif/apply.sh index f7f1f17ade..7194b4fa3b 100644 --- a/openwrt/package/webif/files/usr/lib/webif/apply.sh +++ b/openwrt/package/webif/files/usr/lib/webif/apply.sh @@ -1,14 +1,29 @@ cd /tmp/.webif + +# file-* other config files +for config in $(ls file-* 2>&-); do + name=${config#file-} + echo "Processing config file: $name" + case "$name" in + hosts) mv $config /etc/hosts;; + ethers) mv $config /etc/ethers;; + *) # FIXME: add other config handlers + ;; + esac +done + + +# config-* simple config files [ -f /etc/nvram.overrides ] && ( # White Russian cd /proc/self - cat /tmp/.webif/config-* | tee fd/1 | xargs -n1 nvram set + cat /tmp/.webif/config-* 2>&- | tee fd/1 | xargs -n1 nvram set ) nvram commit -for config in config-*; do +for config in $(ls config-* 2>&-); do name="/usr/lib/webif/apply-${config#config-}.sh" - [ -f "$name" ] && sh $name & + sh $name & done sleep 2 rm -f config-* diff --git a/openwrt/package/webif/files/usr/lib/webif/form.awk b/openwrt/package/webif/files/usr/lib/webif/form.awk index b402285000..554a9090f8 100644 --- a/openwrt/package/webif/files/usr/lib/webif/form.awk +++ b/openwrt/package/webif/files/usr/lib/webif/form.awk @@ -83,5 +83,6 @@ $1 ~ /^end_form/ { if (form_help != "") form_help = "
" form_help "
" print "

Short help:

" form_help form_help_link "
" form_help = "" + form_help_link = "" print "
 
" } diff --git a/openwrt/package/webif/files/usr/lib/webif/webif.sh b/openwrt/package/webif/files/usr/lib/webif/webif.sh index 43f54b0067..40023bd4cc 100644 --- a/openwrt/package/webif/files/usr/lib/webif/webif.sh +++ b/openwrt/package/webif/files/usr/lib/webif/webif.sh @@ -35,6 +35,7 @@ subcategories() { } header() { + CHANGES=$(($( (cat /tmp/.webif/config-* ; ls /tmp/.webif/file-*) 2>&- | wc -l))) ERROR=${ERROR:+

$ERROR



} SAVED=${SAVED:+: Settings saved} _category="$1" @@ -134,10 +135,9 @@ EOF } footer() { - _changes=$(($(cat /tmp/.webif/config-* 2>&- | wc -l))) - _changes=${_changes#0} - _changes=${_changes:+(${_changes})} - cat <
@@ -232,3 +232,19 @@ save_setting() { [ "$oldval" != "$3" ] && echo "$2=\"$3\"" >> /tmp/.webif/config-$1 rm -f /tmp/.webif/config-$1-old } + + +# common awk code for forms +AWK_START_FORM=' + print "
" + print "

" title "

" + print "
" +' +AWK_END_FORM=' + print "
" + if (form_help != "") form_help = "
" form_help "
" + print "

Short help:

" form_help form_help_link "
" + form_help = "" + form_help_link = "" + print "
 
" +' diff --git a/openwrt/package/webif/files/www/cgi-bin/webif/config.sh b/openwrt/package/webif/files/www/cgi-bin/webif/config.sh index f1e8806a5c..7f56d9e173 100755 --- a/openwrt/package/webif/files/www/cgi-bin/webif/config.sh +++ b/openwrt/package/webif/files/www/cgi-bin/webif/config.sh @@ -1,39 +1,43 @@ #!/usr/bin/haserl &- 2>&- -header $FORM_cat . -?> - -

All configuration changes have been cleared.

- - &- 2>&- ?> - -

Updating configuration...

-
-
-

Done

- - - -

Configuration changes:

- &- 2>&- && { - echo -n "

${configname#config-}

"
-			cat $configname
-			echo '
' - } - done - ) ?> - - - -

No configuration changes were made.

- - +case "$CHANGES" in + ""|0)FORM_mode=nochange +esac +case "$FORM_mode" in + nochange) header $FORM_cat . "Configuration: no changes were made.";; + clear) + rm -rf /tmp/.webif >&- 2>&- + header $FORM_cat . "Configuration: cleared." + CHANGES="" + ;; + review) + header $FORM_cat . "Configuration changes:" + cd /tmp/.webif + for configname in config-*; do + grep = $configname >&- 2>&- && { + echo -n "

${configname#config-}


"
+				cat $configname
+				echo '

' + } + done + CONFIGFILES="" + for configname in $(ls file-* 2>&-); do + CONFIGFILES="$CONFIGFILES ${configname#file-}" + done + CONFIGFILES="${CONFIGFILES:+

Config files:

$CONFIGFILES
}" + echo $CONFIGFILES + ;; + save) + header $FORM_cat . "Configuration: updating..." + CHANGES="" + echo '
'
+		sh /usr/lib/webif/apply.sh
+		echo '
' + ;; +esac +footer - +?> diff --git a/openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh b/openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh new file mode 100755 index 0000000000..673818acfa --- /dev/null +++ b/openwrt/package/webif/files/www/cgi-bin/webif/hosts.sh @@ -0,0 +1,148 @@ +#!/usr/bin/haserl + 0) print output + processed = 1 +} +(mode == "add") && (ip == $1) { + print $0 " " name + host_added = 1 + processed = 1 +} +processed == 0 { + print $0 +} +END { + if ((mode == "add") && (host_added == 0)) print ip " " name +}' - < "$HOSTS_FILE" > /tmp/.webif/file-hosts-new + mv "/tmp/.webif/file-hosts-new" "/tmp/.webif/file-hosts" + HOSTS_FILE=/tmp/.webif/file-hosts +} + +update_ethers() { + mkdir -p /tmp/.webif + case "$1" in + add) + grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new + echo "$2 $3" >> /tmp/.webif/file-ethers-new + mv /tmp/.webif/file-ethers-new /tmp/.webif/file-ethers + ;; + del) + grep -E -v "^[ \t]*$2" $ETHERS_FILE > /tmp/.webif/file-ethers-new + mv /tmp/.webif/file-ethers-new /tmp/.webif/file-ethers + ;; + esac + ETHERS_FILE=/tmp/.webif/file-ethers +} + +[ ! -z "$FORM_add_host" ] && { + # add a host to /etc/hosts + validate "ip|FORM_host_ip|IP Address|required|$FORM_host_ip +hostname|FORM_host_name|Hostname|required|$FORM_host_name" && update_hosts add "$FORM_host_ip" "$FORM_host_name" +} +[ ! -z "$FORM_add_dhcp" ] && { + # add a host to /etc/ethers + [ -f /tmp/.webif/file-ethers ] || cat /etc/ethers > /tmp/.webif/file-ethers + validate "mac|FORM_dhcp_mac|MAC Address|required|$FORM_dhcp_mac +ip|FORM_dhcp_ip|IP|required|$FORM_dhcp_ip" && update_ethers add "$FORM_dhcp_mac" "$FORM_dhcp_ip" +} +[ ! -z "$FORM_remove_host" ] && update_hosts del "$FORM_remove_ip" "$FORM_remove_name" +[ ! -z "$FORM_remove_dhcp" ] && update_ethers del "$FORM_remove_mac" + +header "Network" "Hosts" "Configured hosts" '' + +# Hosts in /etc/hosts +# FIXME: move formatting code in form.awk if possible +awk -v "url=$SCRIPT_NAME" ' +BEGIN { + FS="[ \t]" + title = "Hostnames" + '"$AWK_START_FORM"' + print "" + print "" + print "" +} + +# only for valid IPv4 addresses +($1 ~ /^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/) { + gsub(/#.*$/, ""); + output = "" + names_found = 0 + n = split($0, names, "[ \t]") + first = 1 + for (i = 2; i <= n; i++) { + if (names[i] != "") { + if (first != 1) output = output "" + output = output "" + first = 0 + names_found++ + } + } + if (names_found > 0) { + print "" output + print "" + } +} + +END { + print "" + print "" + print "" + print "
IPHostname

" names[i] "Remove
" $1 "

" + '"$AWK_END_FORM"' +} +' $HOSTS_FILE + +# Static DHCP mappings (/etc/ethers) +# FIXME: move formatting code in form.awk if possible +awk -v "url=$SCRIPT_NAME" ' +BEGIN { + FS="[ \t]" + title = "Static IP addresses (for DHCP)" + '"$AWK_START_FORM"' + print "
" + print "" + print "" +} + +# only for valid MAC addresses +($1 ~ /^[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]$/) { + gsub(/#.*$/, ""); + print "" +} + +END { + print "" + print "
MAC addressIP
" $1 "" $2 "Remove
" + print "
" + '"$AWK_END_FORM"' +} +' - < $ETHERS_FILE + +footer ?> + diff --git a/openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh b/openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh index fbf0d79d68..4aae81fdab 100755 --- a/openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh +++ b/openwrt/package/webif/files/www/cgi-bin/webif/upgrade.sh @@ -36,7 +36,6 @@ strip_cybertan() { } ?> -
diff --git a/openwrt/package/webif/files/www/webif.css b/openwrt/package/webif/files/www/webif.css index 947bcb1ac5..35799016bd 100644 --- a/openwrt/package/webif/files/www/webif.css +++ b/openwrt/package/webif/files/www/webif.css @@ -36,6 +36,10 @@ hr { display: none; } +hr.separator { + display: block; +} + .selected-maincat { font-weight: bold; } @@ -199,6 +203,7 @@ hr { } #content .settings-block .settings .settings-content { + padding-top: 1em; margin-left: 1em; float: left; width: 58%; @@ -211,6 +216,7 @@ hr { #content .settings-block .settings .settings-help { padding: 0.4em; + padding-top: 1.4em; margin-left: 42%; } -- 2.30.2